package org.unicen.exa.io.worker.assigner.input;

import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.unicen.exa.io.worker.assigner.core.model.Job;
import org.unicen.exa.io.worker.assigner.core.model.ModelBuilder;
import org.unicen.exa.io.worker.assigner.dao.JobDao;

@Component
public class CreatorJobs implements Runnable {

	private static final Logger logger = Logger.getLogger(CreatorJobs.class);

	private @Autowired JobDao<Job> jobDao;
	private Integer timeInSecond;

	public CreatorJobs() {
	}

	@Value("#{appProperties.builderJobEveryTimeInSecond}")
	public void setTimeInSecond(Integer timeInSecond) {
		this.timeInSecond = timeInSecond;
	}


	public void run() {
		Random random = new Random();
		long id=1l;
		while(true){
			try {
				Float weight = Float.parseFloat(String.format("%s.%s", random.nextInt(5), random.nextInt(9)));
				Job job = ModelBuilder.buildJob(id, 1, 1, weight);
				logger.debug(String.format("INCOMING JOB with id=%s and weight=%s", job.getId(), job.getWeight().toString()));
				jobDao.post(job.getId(), job);
				Thread.sleep(timeInSecond * 1000);
				id++;
			} catch (InterruptedException e) {
				e.printStackTrace();
				logger.error(e);
			}
		}
	}

}
